package com.Smart.smartscals.Fragment;

import java.util.HashMap;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.Smart.smartscals.Activity.ActivityGuide;
import com.Smart.smartscals.Activity.R;
import com.Smart.smartscals.Entity.EntityDowload;
import com.Smart.smartscals.HttpTools.AscyTaskTools;
import com.Smart.smartscals.HttpTools.DownLoadTools;
import com.Smart.smartscals.HttpTools.HttpTools;
import com.Smart.smartscals.HttpTools.ParserJsonTools;
import com.Smart.smartscals.HttpTools.ValidateFormat;
import com.Smart.smartscals.Utils.Const;
import com.Smart.smartscals.Utils.SharePreferceTools;
import com.Smart.smartscals.Utils.Tools;

public class FragmentGudeRegister extends BaseFragment {

	private EditText editTextName, editTextPassWord;
	private Button btnRegister;
	private AscyTaskTools mAscyTaskTools;
	private DownLoadTools mDownLoadTools;
	private EntityDowload mEntityDowload;
	private SharePreferceTools mPreferceTools;
	private View mProgressBar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initView();
		initParam();
		setListener();
	}

	private void initParam() {
		mActivityGuide = (ActivityGuide) this.getActivity();
		editTextName.setText(mActivityGuide.getmEntityUserInfo().getUserName());

		// 解析注册过程；
		mEntityDowload = new EntityDowload(mActivityGuide,
				HttpTools.URLREGISTER, HttpTools.REQUESTPOST,
				ValidateFormat.registDataFormat(mActivityGuide
						.getmEntityUserInfo()), HttpTools.HTTPAPIHTTPCLIENT,
				"qq");
		mDownLoadTools = new DownLoadTools(mEntityDowload);
		mPreferceTools = new SharePreferceTools(mActivityGuide,
				Const.SHAREPREFERENCENAME);

	}

	private void setListener() {
		layBack.setOnClickListener(this);
		btnRegister.setOnClickListener(this);
	}

	private void initView() {
		initTopImgBtn();
		initToastTvBigSmall();
		editTextName = (EditText) mView
				.findViewById(R.id.guide_register_edit_name);

		editTextPassWord = (EditText) mView
				.findViewById(R.id.guide_register_edit_pass);
		btnRegister = (Button) mView.findViewById(R.id.btn_guide_registe);
		setTextSmallBig(R.string.guide_regis, R.string.guide_regisornot);
		mProgressBar =  mView.findViewById(R.id.progressbar_view);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_guide_register, null);
		return mView;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);

		switch (v.getId()) {
		case R.id.btn_guide_registe:
			registerCountName();
			break;
		case R.id.guide_linelayout_top_back:
			backDoJob(this);
			break;
		default:
			break;
		}
	}

	/*
	 * 注册账号姓名
	 */
	private void registerCountName() {
		
		//点击后设置相关控件不能操作
		setOperateAble(false);
		mProgressBar.setVisibility(View.VISIBLE);
		setUserNamePass();
		new AsyncTask<String, Integer, String>() {

			@Override
			protected String doInBackground(String... params) {

				if (mDownLoadTools.startTration()) {
					return mDownLoadTools.getStrFromNet();
				}
				return null;
			}

			protected void onPostExecute(String result) {

				Tools.setLog("--->registe:" + result);

				if (result != null) {

					// 解析
					HashMap<String, String> map = ParserJsonTools
							.parserRegistJson(result);

					if (map.get("success") != null
							&& map.get("success").equals("true")) {
						Tools.setToast(
								mActivityGuide,
								mActivityGuide
										.getString(R.string.guide_toast_register_successful));
						saveRelateDate(map);
					} else {
						
						Tools.setToast(
								mActivityGuide,
								mActivityGuide
										.getString(R.string.guide_toast_register_failed_checksusername));

						
					}

				} else {
					Tools.setToast(
							mActivityGuide,
							mActivityGuide
									.getString(R.string.guide_toast_register_failed_checknetwork));
				}
				setOperateAble(true);
				mProgressBar.setVisibility(View.GONE);
			};
		

		}.execute("");
		
		
	

	}
	/**
	 * 设置一些控件是否可以操作
	 * @param b
	 */
	private void setOperateAble(boolean b) {
		mActivityGuide.setBackClckable(b);
		layBack.setEnabled(b);
		btnRegister.setEnabled(b);
	}

	protected void saveRelateDate(HashMap<String, String> map) {
		// 保存相关的参数；
		mPreferceTools.setString(Const.KEYAUTHKEY, map.get("authkey"));
		mPreferceTools.setString(Const.KEYUSERNAME, mActivityGuide
				.getmEntityUserInfo().getUserName());
		mPreferceTools.setString(Const.KEYPASSWORD, mActivityGuide
				.getmEntityUserInfo().getPassword());
		Tools.startActivity(mActivityGuide, mActivityGuide,
				com.Smart.smartscals.Activity.ActivityMain.class);
	}
	/*
	 * 保存登陆信息
	 */
	private void setUserNamePass() {
		mActivityGuide.getmEntityUserInfo().setPassword(
				editTextPassWord.getText().toString());
		mActivityGuide.getmEntityUserInfo().setUserName(
				editTextName.getText().toString());
	}
}
